<?php
declare (strict_types = 1);

namespace app\controller;


use app\common\model\Content;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\Request;
use think\exception\ValidateException;
use QL\QueryList;

class News extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        if($user["username"] == 'admin'){
            $where = 1;
        }else{
            $where["uid"] = $user["id"];
        }

        $list = \app\common\model\News::with(['user'])
            ->where($where)
            ->order("id DESC")->paginate();

        View::assign("list",$list);
        View::assign("title","我的动态");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        View::assign("title","微资讯发布");
        return \view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $d = $request->post();
        try {
            validate(\app\common\validate\News::class)->check($d);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return json(["code"=>4000,"msg"=>$e->getError()]);
        }
        $result = validate()->check($d);

        $content_mod = new Content();

        $d["author"] = $user["nickname"];
        $d["model_id"] = 5; //动态

        $ql = QueryList::html($d["info"]);
        // 获取a元素对象
        $d["title"]  = $ql->find('p:eq(0)')->text();//第一行为标题


        $temp = $content_mod->save($d);


        $mod = new \app\common\model\News();
        $d["contentid"] = $content_mod->id;
        $d["status"] = 1;
        $d["uid"] = $user["id"];

        $d["userinfo"] = json_encode($user);



        $res = $mod->save($d);
        if($res){
            return  json(["code"=>200,"msg"=>"添加成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

        $article = Db::name("article")->find($id);
        View::assign("article",$article);

        if($user = Session::get("index_user")){
            $uid = $user["id"];
            $order = Db::name('article_order')->where(['aid'=>$article['id'],'uid'=>$uid,'status'=>2])->find();
            if($order){

                View::assign('order',$order);
            }else{
                View::assign('order',0);
            }
        }else{

            View::assign('order',0);
        }

        // 上一篇文章和下一篇文章
        $pre_article = Db::name("article")->where("id","<",$id)->order("id DESC")->find();
        $next_article = Db::name("article")->where("id",">",$id)->order("id ASC")->find();

        View::assign('pre_article',$pre_article);
        View::assign('next_article',$next_article);

        return view('read');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = \app\common\model\News::destroy($id);
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }


}
